﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;

namespace KTX
{
    public partial class frmDienNuoc : DevExpress.XtraEditors.XtraForm
    {
        public frmDienNuoc()
        {
            InitializeComponent();
        }
        Data.QLKTXEntities db = new Data.QLKTXEntities();
        private void frmDienNuoc_Load(object sender, EventArgs e)
        {
            phongBindingSource.DataSource = db.Phong;
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            Tim();
        }
        private void Tim()
        {
            var Phong = (Data.Phong)this.phongBindingSource.Current;
            if (Phong != null)
            {
                dienNuocBindingSource.DataSource = db.DienNuoc.Where(p => p.MaPhong == Phong.MaPhong);
            }
        }
        private void phongBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            Tim();
        }

        private void btnChiTiet_Click(object sender, EventArgs e)
        {
            if (this.dienNuocBindingSource.Current!=null)
            {
                var DienNuoc=(Data.DienNuoc)this.dienNuocBindingSource.Current;
                var f = new frmChiTietDienNuoc(DienNuoc,db);
                f.ShowDialog();
                timchitietthang();
            }
            
        }

        private void btnThemMoi_Click(object sender, EventArgs e)
        {
            
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.chiTietThangBindingSource.RemoveCurrent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            this.dienNuocBindingSource.AddNew();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            db.SaveChanges();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            this.dienNuocBindingSource.RemoveCurrent();
        }

        void timchitietthang()
        {
            if (dienNuocBindingSource.Current != null)
            {
                var dn = (Data.DienNuoc)dienNuocBindingSource.Current;
                chiTietThangBindingSource.DataSource = db.ChiTietThang.Where(p => p.MaDienNuoc == dn.MaDienNuoc);
            }
            else
            {
                chiTietThangBindingSource.DataSource = typeof(Data.ChiTietThang);
            }
        }
        private void dienNuocBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            timchitietthang();
        }

        private void dienNuocBindingSource_AddingNew(object sender, AddingNewEventArgs e)
        {
            if (phongBindingSource.Current!=null)
            {
                var phong = (Data.Phong)phongBindingSource.Current;
                e.NewObject = new Data.DienNuoc()
                {
                    Phong = phong,
                    NamHoc = DateTime.Now.Year,
                    Ngay =DateTime.Now
                };
            }
           
        }

        private void chiTietThangBindingSource_CurrentChanged(object sender, EventArgs e)
        {

        }

        private void btnXoa2_Click(object sender, EventArgs e)
        {
            this.chiTietThangBindingSource.RemoveCurrent();
        }

        private void gridView2_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Clicks==2 && this.chiTietThangBindingSource.Current!=null)
            {
                new frmChiTietDienNuoc((Data.ChiTietThang)this.chiTietThangBindingSource.Current,db).ShowDialog();
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (this.chiTietThangBindingSource.Current != null)
            {
                var report = new BaoCao.xDanhSachChiTiet();
                BindingSource bs = (BindingSource)report.DataSource;
                bs.DataSource = ((Data.DienNuoc)(dienNuocBindingSource.Current)).ChiTietThang;
                report.ShowPreviewDialog();
            }
        }

        private void btnDong_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}